//
//  CXLEmoticonGroup.swift
//  Emotion
//
//  Created by 曹晓龙 on 15/10/23.
//  Copyright © 2015年 曹晓龙. All rights reserved.
//

import UIKit

/// 表情图片模型
class CXLEmoticonGroup: NSObject {
    
    /// 表情图片数组所在的文件夹
    var id:String?
    
    /// 对应toolbar 的显示名
    var group_name_cn:String?
    
    /// 存放图片的数组
    lazy var emoticons = [CXLEmoticon]()
    
    init(dict:[String: AnyObject]){
        
        super.init()
        
        id = dict["id"] as? String
        
        group_name_cn = dict["group_name_cn"] as? String

        if let array = dict["emoticons"] as? [[String : String]] {
            
            var index = 0
            
            for var d in array {
                
                if let png = d["png"], dir = id {
                    
                    d["png"] = dir + "/" + png
                }
                emoticons.append(CXLEmoticon(dict: d))
                
                index++
                
                if index == 20 {
                    
                    emoticons.append(CXLEmoticon(isRemoveBtn: true))
                    
                    index = 0
                }
            }
        }
        addEmptyBtn()
    }
    
    
    private func addEmptyBtn(){
        
        let count = emoticons.count % 21
        
        if emoticons.count > 0 && count == 0 {
            return
        }
                
        for _ in count..<20 {
            
            emoticons.append(CXLEmoticon(isEmptyBtn: true))
        }
        
        emoticons.append(CXLEmoticon(isRemoveBtn: true))
    }
    
    override var description:String {
        let keys = ["id", "group_name_cn", "emoticons"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
}
